package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.NewsReqDTO;
import com.iafme.dto.res.NewsResDTO;
import com.iafme.model.News;
import com.iafme.service.NewsService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/25 09:59
 */
@RestController
@RequestMapping(value = "/news")
@AllArgsConstructor
public class NewsController implements BaseCommonInterface<NewsReqDTO, NewsResDTO> {

    private final NewsService newsService;

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "新闻信息")
    @SaCheckPermission("website:news:add")
    @Override
    public Result<String> save(@Valid NewsReqDTO newsReqDTO) {
        News news = Convert.convert(News.class, newsReqDTO);
        newsService.save(news);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "新闻信息")
    @SaCheckPermission("website:news:edit")
    @Override
    public Result<String> update(@Valid NewsReqDTO newsReqDTO) {
        News news = Convert.convert(News.class, newsReqDTO);
        newsService.updateById(news);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "新闻信息")
    @SaCheckPermission("website:news:delete")
    @Override
    public Result<String> del(Integer id) {
        newsService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<NewsResDTO>> lists(NewsReqDTO newsReqDTO) {
        return Result.successResult(newsService.lists(newsReqDTO));
    }

    @Override
    public Result<NewsResDTO> findById(Integer integer) {
        return null;
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "新闻信息")
    @SaCheckPermission("website:news:delete")
    @Override
    public Result<String> batchDel(List<NewsResDTO> list) {
        List<Integer> ids = list.stream().map(NewsResDTO::getId).collect(Collectors.toList());
        newsService.removeByIds(ids);
        return Result.successResult();
    }
}
